package com.ruoyi.web.controller.report;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.domain.ReportDataset;
import com.ruoyi.report.service.IReportDatasetService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报表数据集Controller
 * 
 * @author sunyan
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/report/dataset")
public class ReportDatasetController extends BaseController
{
    @Autowired
    private IReportDatasetService reportDatasetService;

    /**
     * 查询报表数据集列表
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportDataset reportDataset)
    {
        startPage();
        List<ReportDataset> list = reportDatasetService.selectReportDatasetList(reportDataset);
        return getDataTable(list);
    }

    /**
     * 导出报表数据集列表
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:export')")
    @Log(title = "报表数据集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportDataset reportDataset)
    {
        List<ReportDataset> list = reportDatasetService.selectReportDatasetList(reportDataset);
        ExcelUtil<ReportDataset> util = new ExcelUtil<ReportDataset>(ReportDataset.class);
        util.exportExcel(response, list, "报表数据集数据");
    }

    /**
     * 获取报表数据集详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportDatasetService.selectReportDatasetById(id));
    }

    /**
     * 新增报表数据集
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:add')")
    @Log(title = "报表数据集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportDataset reportDataset)
    {
        int l = reportDatasetService.insertReportDataset(reportDataset);
        return AjaxResult.success(reportDataset.getId());
    }

    /**
     * 修改报表数据集
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:edit')")
    @Log(title = "报表数据集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportDataset reportDataset)
    {
        return toAjax(reportDatasetService.updateReportDataset(reportDataset));
    }

    /**
     * 删除报表数据集
     */
    @PreAuthorize("@ss.hasPermi('report:dataset:remove')")
    @Log(title = "报表数据集", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportDatasetService.deleteReportDatasetByIds(ids));
    }

    /**
     * 校验数据源编码
     */
    @PostMapping("checkCode")
    public AjaxResult checkDataSetCode(@RequestBody String code){
        return AjaxResult.success(reportDatasetService.getReportDataSetCodeList(code));
    }
}
